package com.example.treehole.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.treehole.AddCourseActivity;
import com.example.treehole.R;
import com.example.treehole.bean.Course;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.CourseViewHolder> {
    private List<Course> courseList;

    public CourseAdapter(List<Course> courseList) {
        this.courseList = courseList;
    }

    @NonNull
    @Override
    public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course, parent, false);
        return new CourseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CourseViewHolder holder, int position) {
        Course course = courseList.get(position);
        holder.courseName.setText(course.getName());
        holder.courseTime.setText(course.getTime());
        holder.courseLocation.setText(course.getLocation());

        holder.courseName.setTextColor(Color.WHITE);
        holder.courseTime.setTextColor(Color.WHITE);
        holder.courseLocation.setTextColor(Color.WHITE);
        if (course.getType() == 1) {
            holder.itemView.setBackgroundColor(getRandomColor());
            holder.courseName.setVisibility(View.VISIBLE);
            holder.courseTime.setVisibility(View.VISIBLE);
            holder.courseLocation.setVisibility(View.VISIBLE);
        } else if (course.getType() == 2) {
            holder.itemView.setBackgroundColor(Color.parseColor("#f6f6f6"));
            holder.courseName.setVisibility(View.INVISIBLE);
            holder.courseTime.setVisibility(View.INVISIBLE);
            holder.courseLocation.setVisibility(View.INVISIBLE);
        } else if (course.getType() == 3) {
            holder.itemView.setBackgroundColor(Color.parseColor("#3AD0F9"));
            holder.courseName.setVisibility(View.VISIBLE);
            holder.courseTime.setVisibility(View.GONE);
            holder.courseLocation.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (course.getType() == 1) {
                    new AlertDialog.Builder(holder.itemView.getContext())
                            .setTitle("删除课程")
                            .setMessage("确定要删除吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    HashMap<String, Object> params = new HashMap<>();
                                     params.put("id", course.getId());
                                    HttpUtil.delete(Api.COURSE_DELETE, params, new HttpUtil.OnNetStringListener() {
                                        @Override
                                        public void success(String data, String message) {
                                            // 删除课程的逻辑
                                            courseList.set(holder.getAbsoluteAdapterPosition(), Course.makeEmptyCourse(course.getDayOfWeek(), course.getClassNumber()));
                                            notifyDataSetChanged();
                                        }

                                        @Override
                                        public void error(String message) {

                                        }
                                    });
                                }
                            })
                            .setNegativeButton("取消", null)
                            .show();
                } else if (course.getType() == 2) {
                    new AlertDialog.Builder(holder.itemView.getContext())
                            .setTitle("新增课程")
                            .setMessage("确定要新增吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    int dayOfWeek = holder.getAbsoluteAdapterPosition() % 5+1;
                                    int classNumber = holder.getAbsoluteAdapterPosition() / 5;
                                    Context context = holder.itemView.getContext();
                                    Intent intent = new Intent(context, AddCourseActivity.class);
                                    intent.putExtra("dayOfWeek", dayOfWeek);
                                    intent.putExtra("classNumber", classNumber);
                                    context.startActivity(intent);
                                }
                            })
                            .setNegativeButton("取消", null)
                            .show();
                }
            }
        });
    }

    private int getRandomColor() {
        int r = new Random().nextInt(255);
        int g = new Random().nextInt(255);
        int b = new Random().nextInt(255);
        return Color.rgb(r, g, b);
    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }

    public static class CourseViewHolder extends RecyclerView.ViewHolder {
        TextView courseName;
        TextView courseTime;
        TextView courseLocation;

        public CourseViewHolder(@NonNull View itemView) {
            super(itemView);
            courseName = itemView.findViewById(R.id.course_name);
            courseTime = itemView.findViewById(R.id.course_time);
            courseLocation = itemView.findViewById(R.id.course_location);
        }
    }
}